package com.example.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.dao.AdUserDao;
import com.example.admin.service.AdUserService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.dtos.UserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import com.usian.utils.common.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class AdUserServiceImpl implements AdUserService {
    @Autowired
    private AdUserDao adUserDao;

    @Override
    public Integer addUser(UserDto userDto) {
        AdUser adUser = new AdUser();
        BeanUtils.copyProperties(userDto,adUser);
        adUser.setCreatedTime(new Date());
        adUser.setStatus(1);
        Random random = new Random();
        int i = random.nextInt(1000);
        adUser.setSalt("abc"+i);
        return adUserDao.insert(adUser);

    }

    @Override
    public void deleteUser(Integer id) {

    }

    @Override
    public void updateUser(UserDto userDto) {

    }

    @Override
    public List<AdUser> getUserByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",name);
        //定义页码，每页显示的条数
        IPage<AdUser> page = new Page<>(1,2);
        IPage pageResult = adUserDao.selectPage(page, queryWrapper);
        //获取分页以后的记录
        List records = pageResult.getRecords();
        return records;
    }

    @Override
    public List<AdUser> getUsers() {
        return null;
    }

    @Override
    public ResponseResult login(AdUserDto adUserDto) {
        QueryWrapper<AdUser> queryWrapper = new QueryWrapper<AdUser>();
        queryWrapper.eq("name",adUserDto.getName());
        //1查询用户表
        List<AdUser> userList = adUserDao.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(userList) && userList.size()==1){
            AdUser adUser = userList.get(0);
            String pass = MD5Utils.encode(adUserDto.getPassword() + adUser.getSalt());
            if(pass.equals(adUser.getPassword())){
                HashMap map  =new HashMap();
                String token = AppJwtUtil.getToken(adUser.getId().longValue());
                map.put("token",token);
                adUser.setSalt("");
                adUser.setPassword("");
                map.put("user",adUser);
                return ResponseResult.okResult(map);
            }
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);


    }
}
